package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {

    @Resource
    private CheckGroupService checkGroupService;
    @Resource
    private CheckItemService checkItemService;

    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS,pageResult);
    }

    @PostMapping("/add")
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup ,@RequestParam List<Integer> checkitemIds){
        if (checkitemIds.size() < 2){
            throw new BusinessRuntimeException("请选择最少两项检查项");
        }
        checkGroupService.addCheckGroup(checkGroup,checkitemIds);
        return new Result(true ,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }
    @GetMapping("/findAll")
    public Result findAll(){
        List<CheckGroup> list = checkGroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }
    /**
     * 根据id获取检查组信息
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result selectGroupById(Integer id){
        CheckGroup checkGroup = checkGroupService.selectGroupById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
    }

    /**
     * 根据groupId查询对应的itemId集合
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result selectItemIdsByCheckGroupId(Integer id){
        List<Integer> itemIds = checkGroupService.selectItemIdsByCheckGroupId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,itemIds);
    }

    @PostMapping("/edit")
    public Result updateGroup(@RequestBody CheckGroup checkGroup, @RequestParam List<Integer> checkitemIds){
        checkGroupService.updateGroup(checkGroup,checkitemIds);
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(Integer id){
        checkGroupService.deleteCheckGroupitemById(id);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }
}
